package com.lear.njava.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lear.njava.pojo.Grade;
import com.lear.njava.service.GradeService;
import com.lear.njava.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "年级控制器") //controller介绍
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {

    @Autowired //注入GradeService
    private GradeService gradeService;

    //sms/gradeController/getGrades
    @ApiOperation("获取全部年级")
    @GetMapping("/getGrades")
    public Result getGrades(){
        List<Grade> grades = gradeService.getGrades();
        return Result.ok(grades);
    }

    //sms/gradeController/deleteGrade
    @DeleteMapping("/deleteGrade")
    @ApiOperation("删除Grade信息") // 介绍接口
    public Result deleteGrade(
            @ApiParam("所有要删除的Grade的id的JSON集合") @RequestBody List<Integer> ids){ //用List接收参数 //介绍api参数
        gradeService.removeByIds(ids); //IService的实现类ServiceImpl已经实现
        return Result.ok();
    }

    //sms/gradeController/saveOrUpdateGrade
    @ApiOperation("新增或者修改Grade，有id为修改，没有则为增加") //介绍接口
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(
            @ApiParam("JSON的Grade对象") @RequestBody Grade grade // 将传入的json对象赋给grade
    ){
        // 1.接收参数
        // 2.调用服务层方法完成增加或者修改
        gradeService.saveOrUpdate(grade); // mybatis-plus的IService的实现类ServiceImpl已经实现了saveOrUpdate
        return Result.ok();
    }

    //sms/gradeController/getGrades/1/3?gradeName=xxxx
    @GetMapping("/getGrades/{pageNo}/{pageSize}") //将占位符解析成参数
    @ApiOperation("根据年级名称模糊查询，带分页")
    public Result getGrades(
            @ApiParam("分页查询的页码数") @PathVariable("pageNo") Integer pageNo, //将路径参数经过PathVariable解析赋给pageNo
            @ApiParam("分页查询的页大小") @PathVariable("pageSize") Integer pageSize,
            @ApiParam("分页查询模糊匹配的名称") String gradeName // !!!!!踩坑，加上RequestParam注解为必选参数，不加为可选
            // @RequestParam("gradeName") String gradeName //后面参数名保持一致可以不写RequestPram注解
    ){
        // 1.分页 待条件查询
        // 2.封装Result对象并返回
        // 1.
        Page<Grade> page = new Page<Grade>(pageNo, pageSize); // 创建page对象
        // 通过服务层查询，控制层只负责参数的接收和前后传递
        IPage<Grade> pageRs = gradeService.getGradeByOpr(page, gradeName); // 将查询结果返回给IPage对象
        // 2.
        return Result.ok(pageRs);
    }
}
